﻿using Autofac;
using Autofac.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Ongoal.Quotation.Middleware;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation.APIExtensions
{
    /// <summary>
    /// 公共注入
    /// </summary>
    public static class WebApplicationBuilderExtensions
    {
        /// <summary>
        /// 公共注入
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static WebApplication UseCommon(this WebApplicationBuilder builder, Action<IServiceCollection> Before, Action<ContainerBuilder> RegAction)
        {
            builder.Host.UseAutofac();
            builder.Services.UseFramework(builder.Configuration, Before, RegAction);
            builder.Services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true)
                     .Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);

            var app = builder.Build();
            app.UseEnableRequestRewind();
            IHostApplicationLifetime hostApplicationLifetime = app.Services.GetService<IHostApplicationLifetime>();

            app.UseCommon(builder.Configuration, hostApplicationLifetime);
            return app;
        }
    }
}
